import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class SoundManager {

	public PCMFilePlayer pcmFilePlayer;

	private boolean mute = false;
	
	public SoundManager() {
		try {
			pcmFilePlayer = new PCMFilePlayer(getClass().getResource("sounds/background.wav"));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (UnsupportedAudioFileException e) {
			e.printStackTrace();
		} catch (LineUnavailableException e) {
			e.printStackTrace();
		}
		
	}
	
	public void changeSelectedSound() {
		if (!mute) {
			Clip clip;
			try {
				clip = AudioSystem.getClip();

				AudioInputStream inputStream = AudioSystem
						.getAudioInputStream(getClass().getResource(
								"sounds/coin.wav"));
				clip.open(inputStream);
				clip.start();
			} catch (LineUnavailableException e) {
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void wrongAnswer() {
		if (!mute) {
			Clip clip;
			try {
				clip = AudioSystem.getClip();

				AudioInputStream inputStream = AudioSystem
						.getAudioInputStream(getClass().getResource(
								"sounds/wrongAnswer.wav"));
				clip.open(inputStream);
				clip.start();
			} catch (LineUnavailableException e) {
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void winGame() {
		if (!mute) {
			Clip clip;
			try {
				clip = AudioSystem.getClip();

				AudioInputStream inputStream = AudioSystem
						.getAudioInputStream(getClass().getResource(
								"sounds/winGame.wav"));
				clip.open(inputStream);
				clip.start();
			} catch (LineUnavailableException e) {
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void correctAnswer() {
		if (!mute) {
			Clip clip;
			try {
				clip = AudioSystem.getClip();

				AudioInputStream inputStream = AudioSystem
						.getAudioInputStream(getClass().getResource(
								"sounds/correctAnswer.wav"));
				clip.open(inputStream);
				clip.start();
			} catch (LineUnavailableException e) {
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void diceRoll() {
		if (!mute) {
			Clip clip;
			try {
				clip = AudioSystem.getClip();

				AudioInputStream inputStream = AudioSystem
						.getAudioInputStream(getClass().getResource(
								"sounds/diceRoll.wav"));
				clip.open(inputStream);
				clip.start();
			} catch (LineUnavailableException e) {
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void StartBackground() {
		if (!mute && pcmFilePlayer != null) {
			pcmFilePlayer.start();
			System.out.println("starting background");
		}
	}

	public void playSelectNoise() {
		if (!mute) {
			try {
				Clip clip = AudioSystem.getClip();
				AudioInputStream inputStream = AudioSystem
						.getAudioInputStream(getClass().getResource(
								"sounds/select.wav"));
				clip.open(inputStream);
				clip.start();
			} catch (LineUnavailableException e) {
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				Thread.currentThread().sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void toggleSound() {
		mute = !mute;
		if (!mute) {
			pcmFilePlayer.start();
		} else {
			pcmFilePlayer.stop();
		}
	}

	public boolean isMuted() {
		return mute;
	}

	public void playStartSound() {
		if (!mute) {
			Clip clip;
			try {
				clip = AudioSystem.getClip();
				AudioInputStream inputStream = AudioSystem
						.getAudioInputStream(getClass().getResource(
								"sounds/start.wav"));
				clip.open(inputStream);
				clip.start();
			} catch (LineUnavailableException e1) {
				e1.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void playExitSound() {
		if (!mute) {
			try {
				Clip clip = AudioSystem.getClip();
				AudioInputStream inputStream = AudioSystem
						.getAudioInputStream(getClass().getResource(
								"sounds/death.wav"));
				clip.open(inputStream);
				clip.start();
			} catch (LineUnavailableException e) {
				e.printStackTrace();
			} catch (UnsupportedAudioFileException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				Thread.currentThread().sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
